package com.example.threadlocal;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolUtil {
    private static ExecutorService executor = Executors.newFixedThreadPool(5);

    private static TransmittableThreadLocal<String> context = new TransmittableThreadLocal<String>();


    public static void submit(RunnableContext runnableContext) {
        context.set(runnableContext.getLogId());
        executor.submit(TtlRunnable.get(() -> {
            String s = context.get();
            System.out.println("输出上下文：" + s);
            runnableContext.getRunnable().run();
        }));
    }

    public static <T> Future<T> submit(CallableContext<T> callableContext) {
        context.set(callableContext.getLogId());
        return executor.submit(TtlCallable.get(() -> {
            String s = context.get();
            System.out.println("输出上下文：" + s);
            return callableContext.getCallable().call();
        }));
    }
}
